package DroidEye.DAO;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import DroidEye.HelloApplication;
import DroidEye.DAO.DAOInterface.AdminDAOInterface;
import DroidEye.Dto.Adjudicator;
import DroidEye.Dto.Administrator;
import DroidEye.Dto.Student;
import DroidEye.Dto.Teacher;
import DroidEye.JavaBean.AdminBean;
import DroidEye.Mapper.AdjudicatorMapper;
import DroidEye.Mapper.AdministratorMapper;
import DroidEye.Mapper.StudentMapper;
import DroidEye.Mapper.TeacherMapper;

@Service
public class AdminDAO implements AdminDAOInterface {
    @Autowired
    private AdministratorMapper administratorMapper;
    
    @Autowired
    private AdjudicatorMapper adjudicatorMapper;
    
    @Autowired
    private StudentMapper studentMapper;
    
    @Autowired
    private TeacherMapper teacherMapper;

    @Override
    public boolean createAdminAccount(AdminBean adminBean) {
        if(adminBean == null) {
            return false;
        }

        Administrator administrator = new Administrator();
        administrator.setAdminId(adminBean.getAdminID());
        administrator.setAdminPassword(adminBean.getAdminPassword());
        administrator.setAdminName(adminBean.getAdminName());
        
        int result = administratorMapper.createAdminAccount(administrator);
        if(result > 0) {
            return true;
        }
        return false;
    }

    @Override
    public AdminBean queryAdminAccount(String adminID) {
        Administrator administrator = administratorMapper.queryAdminAccount(adminID);
        if(administrator == null) {
            return null;
        }
        return custAdministrator(administrator);
    }

    @Override
    public boolean createStudentAccount(String studentID, String studentName) {
        Student record = new Student();
        record.setStudentId(studentID);
        record.setStudentName(studentName);
        int result = studentMapper.insertSelective(record);
        if(result == 0) {
            return false;
        }
        return true;
    }

    @Override
    public boolean isHaveStudent(String studentID) {
        Student student = studentMapper.selectByPrimaryKey(studentID);
        if(student == null) {
            return false;
        }
        return true;
    }

    @Override
    public int studentNumber() {
        return studentMapper.countAllRecord();
    }

    @Override
    public boolean createTeacherAccount(String teacherID, String teacherName) {
        Teacher record = new Teacher();
        record.setTeacherId(teacherID);
        record.setTeacherName(teacherName);
        int result = teacherMapper.insertSelective(record);
        if(result == 0) {
            return false;
        }
        return true;
    }

    @Override
    public boolean isHaveTeacher(String teacherID) {
        Teacher teacher = teacherMapper.selectByPrimaryKey(teacherID);
        if(teacher == null) {
            return false;
        }
        return true;
    }

    @Override
    public int teacherNumber() {
        return teacherMapper.countAllRecord();
    }

    @Override
    public boolean createAdjuAccount(String adjuID, String adjuName) {
        Adjudicator adjudicator = new Adjudicator();
        adjudicator.setAdjudicatorId(adjuID);
        adjudicator.setAdjudicatorName(adjuName);
        int result = adjudicatorMapper.insertSelective(adjudicator);
        if(result == 0) {
            return false;
        }
        return true;
    }

    @Override
    public boolean isHaveAdju(String adjuID) {
        Adjudicator adjudicator = adjudicatorMapper.queryAdjuAccount(adjuID);
        if(adjudicator == null) {
            return false;
        }
        return true;
    }

    @Override
    public int adjuNumber() {
        return adjudicatorMapper.countAllRecord();
    }
    
    // 转换Administrator为AdminBean
    private AdminBean custAdministrator(Administrator administrator){
        AdminBean adminBean = new AdminBean();
        adminBean.setAdminID(administrator.getAdminId());
        adminBean.setAdminPassword(administrator.getAdminPassword());
        adminBean.setAdminName(administrator.getAdminName());
        return adminBean;
    }
    
    public static AdminDAO getAdminDAOInstance() {
        return HelloApplication.getBean("adminDAO", AdminDAO.class);
    }
}
